package com.qf.dao.impl;

import com.qf.dao.AccountDao;
import com.qf.pojo.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.SQLException;
import java.util.List;

/**
 * @Description: 公用常量
 * @Data: 2022/9/1 14:56
 * @Pacakge: com.qf.dao.impl
 * @ClassName: AccountDaoImpl
 * @Author: fan
 */
public class AccountDaoImpl implements AccountDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 根据id查找账户信息
     *
     * @param name
     * @return
     */
    public Account findAccountByName(String name) {
        List<Account> list = jdbcTemplate.query("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), name);
        if (list == null || list.size() == 0) {
            return null;
        }
        if (list.size() > 1) {
            throw new RuntimeException();
        }
        return list.get(0);

    }

    /**
     * 修改账户信息
     *
     * @param account
     */
    public void updateAccount(Account account) {

        jdbcTemplate.update("update account set money=? where name=?", account.getMoney(), account.getName());


    }
}